/* Copyright 2008 Bas van den Berg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "yaffut.h"
#include "myassert.h"
#include "Stats.h"
#include "StringBuilder.h"

using namespace std;
using namespace Chronos;

struct StatsTest {
    Stats stats;
};

TEST(StatsTest, testInitialState) {
    ASSERT_EQUAL(Stats::unknown, stats[Stats::maxHealth]);
    ASSERT_EQUAL(Stats::unknown, stats[Stats::hitChance]);
}


TEST(StatsTest, testToStr) {
    ASSERT_STR_EQUAL("max health", stats.toStr[Stats::maxHealth]);
}


TEST(StatsTest, testSettingGetting) {
    stats.set(Stats::maxHealth, 400);
    ASSERT_EQUAL(400, stats[Stats::maxHealth]);
}


TEST(StatsTest, testModify) {
    stats.set(Stats::maxHealth, 100);
    stats.modify(Stats::maxHealth, 10);
    ASSERT_EQUAL(110, stats[Stats::maxHealth]);
    ASSERT_EQUAL(100, stats.getBase(Stats::maxHealth));

    stats.modify(Stats::maxHealth, -30);
    ASSERT_EQUAL(80, stats[Stats::maxHealth]);
    ASSERT_EQUAL(100, stats.getBase(Stats::maxHealth));
}


TEST(StatsTest, testChangingMaxHealth) {
    stats.set(Stats::maxHealth, 100);
    ASSERT_EQUAL(100, stats.getHealth());

    stats.modify(Stats::maxHealth, 10);
    ASSERT_EQUAL(110, stats[Stats::maxHealth]);
    ASSERT_EQUAL(100, stats.getHealth());

    stats.modify(Stats::maxHealth, -20);
    ASSERT_EQUAL(90, stats[Stats::maxHealth]);
    ASSERT_EQUAL(90, stats.getHealth());
}


TEST(StatsTest, testSerialization) {
    stats.set(Stats::maxHealth, 100);
    stats.set(Stats::speed, 10);
    stats.setHealth(80);
    StringBuilder buffer;
    stats.serialize(buffer);

    ASSERT_STR_EQUAL("100,10,-1,-1,80", buffer);
    Stats stats2((string)buffer);
    ASSERT_EQUAL(100, stats2[Stats::maxHealth]);
    ASSERT_EQUAL(10, stats2[Stats::speed]);
    ASSERT_EQUAL(Stats::unknown, stats2[Stats::hitChance]);
    ASSERT_EQUAL(80, stats2.getHealth());
}

/*
TEST(StatsTest, testDeSerializationShouldFail) {
    try {
        Stats stats3("100,10,-1,-1");
    } catch (MudError& e) {
    //    ASSERT_STR_EQUAL("Assert failed", e.what());
        return;
    }
    ASSERT_FAIL();
}
*/
